﻿using Model;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    public class BllDAL
    {
        public List<Bills> GetBillListService(int billType, string beginTime, string endTime, int page, int limit, out int count)
        {
            BillEntities db = new BillEntities();

            var query = db.Bills.AsQueryable();

            //过滤：billType, string beginTime, string endTime,
            if (billType > 0)
                query = query.Where(bill => bill.Bill_Type_Id == billType);

            if (!string.IsNullOrEmpty(beginTime))
            {
                DateTime begin = Convert.ToDateTime(beginTime);

                query = query.Where(c => c.Bill_time > begin);
            }

            if (!string.IsNullOrEmpty(endTime))
            {
                DateTime end = Convert.ToDateTime(endTime);

                query = query.Where(c => c.Bill_time < end);
            }

            //排序和分页：int page, int limit
            count = query.Count();
            query.OrderBy(bill => bill.Id).Skip((page - 1) * limit).Take(limit);


            return query.ToList();
        }

        public int DeleteBatchBillsBLLService(Bills[] bills)
        {
            BillEntities db = new BillEntities();

            foreach (var bill in bills)
            {
                Bills info = db.Bills.Find(bill.Id);

                if (info != null)
                    db.Bills.Remove(info);
            }

            return db.SaveChanges();
        }

        public Bills GetBillssInfoByIdService(int id)
        {
            BillEntities db = new BillEntities();

            return db.Bills.Find(id);
        }

        public int SaveBillsInfoService(Bills bills)
        {
            BillEntities db = new BillEntities();

            if (bills.Id > 0)
                db.Entry<Bills>(bills).State = System.Data.Entity.EntityState.Modified;
            else
                db.Bills.Add(bills);

            return db.SaveChanges();
        }

        public int DeleteService(int id)
        {
            BillEntities db = new BillEntities();

            Bills info = db.Bills.Find(id);

            if (info == null)
                return 0;

            db.Bills.Remove(info);

            return db.SaveChanges();
        }

    }
}
